<template>
  <!-- <vue-particles
      id="tsparticles"
      :particlesInit="particlesInit"
      :particlesloaded="particlesLoaded"
      :options='config1'
  /> -->
  <div class="formContainer">
    <h3>实验室成果管理系统</h3>

    <!--suppress JSValidateTypes -->
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          登录
        </el-button>
      </el-form-item>
      </el-form>
  </div>
</template>


<script setup>
import {useUserStore} from '@/store/useUserStore'
import {useRouter} from 'vue-router'
import {ref,reactive} from 'vue'
// import { loadFull } from "tsparticles";
// import {config1} from "@/unit/config1";
import axios from "axios";
import { ElMessage } from 'element-plus'

//ref获取表单对象
const ruleFormRef = ref()
const ruleForm = reactive({
  username: "",
  password:""
})
const rules = reactive({

  username:[
    {required:true,message:'请输入用户名',trigger:'blur'},
  ],
  password:[
    {required:true,message:'请输入密码',trigger:'blur'},
  ],
})
const {changeUser} = useUserStore()
const router = useRouter()
//登录回调 其中有校验
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
        const res = await axios.post('http://localhost:8000/adminapi/users/login', ruleForm);
        let {code,data} = res.data
      if (code===0){
        sessionStorage.setItem("userInfo",JSON.stringify(data))
        changeUser(data)
        await router.push("mainbox")
      }else{
        ElMessage.error('用户名密码不匹配')
      }
      }
     else {
      console.log('error submit!', fields);
    }
  });
}
// //粒子配置
// const particlesLoaded = async container=> {
//   console.log("Particles container loaded", container);
// };
// const particlesInit = async engine=>{
//   await loadFull(engine);
// }


</script>



<style lang="scss">
.formContainer{
  width: 500px;
  height: 300px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  color: white;
  text-shadow: 2px 2px 5px black;
  text-align: center;
}
</style>




